home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / jpeg / jdmain.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  14KB  |  474 lines

  1. /*
  2.  * jdmain.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains a command-line user interface for the JPEG decompressor.
  9.  * It should work on any system with Unix- or MS-DOS-style command lines.
  10.  *
  11.  * Two different command line styles are permitted, depending on the
  12.  * compile-time switch TWO_FILE_COMMANDLINE:
  13.  *    djpeg [options]  inputfile outputfile
  14.  *    djpeg [options]  [inputfile]
  15.  * In the second style, output is always to standard output, which you'd
  16.  * normally redirect to a file or pipe to some other program.  Input is
  17.  * either from a named file or from standard input (typically redirected).
  18.  * The second style is convenient on Unix but is unhelpful on systems that
  19.  * don't support pipes.  Also, you MUST use the first style if your system
  20.  * doesn't do binary I/O to stdin/stdout.
  21.  */
  22.  
  23. #include "jinclude.h"
  24. #ifdef INCLUDES_ARE_ANSI
  25. #include <stdlib.h>        /* to declare exit() */
  26. #endif
  27. #include <ctype.h>        /* to declare isupper(), tolower() */
  28. #ifdef NEED_SIGNAL_CATCHER
  29. #include <signal.h>        /* to declare signal() */
  30. #endif
  31. #ifdef USE_SETMODE
  32. #include <fcntl.h>        /* to declare setmode() */
  33. #endif
  34.  
  35. #ifdef THINK_C
  36. #include <console.h>        /* command-line reader for Macintosh */
  37. #endif
  38.  
  39. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  40. #define READ_BINARY    "r"
  41. #define WRITE_BINARY    "w"
  42. #else
  43. #define READ_BINARY    "rb"
  44. #define WRITE_BINARY    "wb"
  45. #endif
  46.  
  47. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  48. #define EXIT_FAILURE  1
  49. #endif
  50. #ifndef EXIT_SUCCESS
  51. #ifdef VMS
  52. #define EXIT_SUCCESS  1        /* VMS is very nonstandard */
  53. #else
  54. #define EXIT_SUCCESS  0
  55. #endif
  56. #endif
  57.  
  58.  
  59. #include "jversion.h"        /* for version message */
  60.  
  61.  
  62. /*
  63.  * This list defines the known output image formats
  64.  * (not all of which need be supported by a given version).
  65.  * You can change the default output format by defining DEFAULT_FMT;
  66.  * indeed, you had better do so if you undefine PPM_SUPPORTED.
  67.  */
  68.  
  69. typedef enum {
  70.     FMT_GIF,        /* GIF format */
  71.     FMT_PPM,        /* PPM/PGM (PBMPLUS formats) */
  72.     FMT_RLE,        /* RLE format */
  73.     FMT_TARGA,        /* Targa format */
  74.     FMT_TIFF        /* TIFF format */
  75. } IMAGE_FORMATS;
  76.  
  77. #ifndef DEFAULT_FMT        /* so can override from CFLAGS in Makefile */
  78. #define DEFAULT_FMT    FMT_PPM
  79. #endif
  80.  
  81. static IMAGE_FORMATS requested_fmt;
  82.  
  83.  
  84. /*
  85.  * This routine gets control after the input file header has been read.
  86.  * It must determine what output file format is to be written,
  87.  * and make any other decompression parameter changes that are desirable.
  88.  */
  89.  
  90. METHODDEF void
  91. d_ui_method_selection (decompress_info_ptr cinfo)
  92. {
  93.   /* if grayscale or CMYK input, force similar output; */
  94.   /* else leave the output colorspace as set by options. */
  95.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  96.     cinfo->out_color_space = CS_GRAYSCALE;
  97.   else if (cinfo->jpeg_color_space == CS_CMYK)
  98.     cinfo->out_color_space = CS_CMYK;
  99.  
  100.   /* select output file format */
  101.   /* Note: jselwxxx routine may make additional parameter changes,
  102.    * such as forcing color quantization if it's a colormapped format.
  103.    */
  104.   switch (requested_fmt) {
  105. #ifdef GIF_SUPPORTED
  106.   case FMT_GIF:
  107.     jselwgif(cinfo);
  108.     break;
  109. #endif
  110. #ifdef PPM_SUPPORTED
  111.   case FMT_PPM:
  112.     jselwppm(cinfo);
  113.     break;
  114. #endif
  115. #ifdef RLE_SUPPORTED
  116.   case FMT_RLE:
  117.     jselwrle(cinfo);
  118.     break;
  119. #endif
  120. #ifdef TARGA_SUPPORTED
  121.   case FMT_TARGA:
  122.     jselwtarga(cinfo);
  123.     break;
  124. #endif
  125.   default:
  126.     ERREXIT(cinfo->emethods, "Unsupported output file format");
  127.     break;
  128.   }
  129. }
  130.  
  131.  
  132. /*
  133.  * Signal catcher to ensure that temporary files are removed before aborting.
  134.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  135.  * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  136.  */
  137.  
  138. #ifdef NEED_SIGNAL_CATCHER
  139.  
  140. static external_methods_ptr emethods; /* for access to free_all */
  141.  
  142. GLOBAL void
  143. signal_catcher (int signum)
  144. {
  145.   if (emethods != NULL) {
  146.     emethods->trace_level = 0;    /* turn off trace output */
  147.     (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  148.   }
  149.   exit(EXIT_FAILURE);
  150. }
  151.  
  152. #endif
  153.  
  154.  
  155. /*
  156.  * Optional routine to display a percent-done figure on stderr.
  157.  * See jddeflts.c for explanation of the information used.
  158.  */
  159.  
  160. #ifdef PROGRESS_REPORT
  161.  
  162. METHODDEF void
  163. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  164. {
  165.   if (cinfo->total_passes > 1) {
  166.     fprintf(stderr, "\rPass %d/%d: %3d%% ",
  167.         cinfo->completed_passes+1, cinfo->total_passes,
  168.         (int) (loopcounter*100L/looplimit));
  169.   } else {
  170.     fprintf(stderr, "\r %3d%% ",
  171.         (int) (loopcounter*100L/looplimit));
  172.   }
  173.   fflush(stderr);
  174. }
  175.  
  176. #endif
  177.  
  178.  
  179. /*
  180.  * Argument-parsing code.
  181.  * The switch parser is designed to be useful with DOS-style command line
  182.  * syntax, ie, intermixed switches and file names, where only the switches
  183.  * to the left of a given file name affect processing of that file.
  184.  * The main program in this file doesn't actually use this capability...
  185.  */
  186.  
  187.  
  188. static char * progname;        /* program name for error messages */
  189.  
  190.  
  191. LOCAL void
  192. usage (void)
  193. /* complain about bad command line */
  194. {
  195.   fprintf(stderr, "usage: %s [switches] ", progname);
  196. #ifdef TWO_FILE_COMMANDLINE
  197.   fprintf(stderr, "inputfile outputfile\n");
  198. #else
  199.   fprintf(stderr, "[inputfile]\n");
  200. #endif
  201.  
  202.   fprintf(stderr, "Switches (names may be abbreviated):\n");
  203.   fprintf(stderr, "  -colors N      Reduce image to no more than N colors\n");
  204. #ifdef GIF_SUPPORTED
  205.   fprintf(stderr, "  -gif           Select GIF output format\n");
  206. #endif
  207. #ifdef PPM_SUPPORTED
  208.   fprintf(stderr, "  -pnm           Select PBMPLUS (PPM/PGM) output format (default)\n");
  209. #endif
  210.   fprintf(stderr, "  -quantize N    Same as -colors N\n");
  211. #ifdef RLE_SUPPORTED
  212.   fprintf(stderr, "  -rle           Select Utah RLE output format\n");
  213. #endif
  214. #ifdef TARGA_SUPPORTED
  215.   fprintf(stderr, "  -targa         Select Targa output format\n");
  216. #endif
  217.   fprintf(stderr, "Switches for advanced users:\n");
  218. #ifdef BLOCK_SMOOTHING_SUPPORTED
  219.   fprintf(stderr, "  -blocksmooth   Apply cross-block smoothing\n");
  220. #endif
  221.   fprintf(stderr, "  -grayscale     Force grayscale output\n");
  222.   fprintf(stderr, "  -nodither      Don't use dithering in quantization\n");
  223. #ifdef QUANT_1PASS_SUPPORTED
  224.   fprintf(stderr, "  -onepass       Use 1-pass quantization (fast, low quality)\n");
  225. #endif
  226.   fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
  227.   fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
  228.   exit(EXIT_FAILURE);
  229. }
  230.  
  231.  
  232. LOCAL boolean
  233. keymatch (char * arg, const char * keyword, int minchars)
  234. /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
  235. /* keyword is the constant keyword (must be lower case already), */
  236. /* minchars is length of minimum legal abbreviation. */
  237. {
  238.   register int ca, ck;
  239.   register int nmatched = 0;
  240.  
  241.   while ((ca = *arg++) != '\0') {
  242.     if ((ck = *keyword++) == '\0')
  243.       return FALSE;        /* arg longer than keyword, no good */
  244.     if (isupper(ca))        /* force arg to lcase (assume ck is already) */
  245.       ca = tolower(ca);
  246.     if (ca != ck)
  247.       return FALSE;        /* no good */
  248.     nmatched++;            /* count matched characters */
  249.   }
  250.   /* reached end of argument; fail if it's too short for unique abbrev */
  251.   if (nmatched < minchars)
  252.     return FALSE;
  253.   return TRUE;            /* A-OK */
  254. }
  255.  
  256.  
  257. LOCAL int
  258. parse_switches (decompress_info_ptr cinfo, int last_file_arg_seen,
  259.         int argc, char **argv)
  260. /* Initialize cinfo with default switch settings, then parse option switches.
  261.  * Returns argv[] index of first file-name argument (== argc if none).
  262.  * Any file names with indexes <= last_file_arg_seen are ignored;
  263.  * they have presumably been processed in a previous iteration.
  264.  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  265.  */
  266. {
  267.   int argn;
  268.   char * arg;
  269.  
  270.   /* (Re-)initialize the system-dependent error and memory managers. */
  271.   jselerror(cinfo->emethods);    /* error/trace message routines */
  272.   jselmemmgr(cinfo->emethods);    /* memory allocation routines */
  273.   cinfo->methods->d_ui_method_selection = d_ui_method_selection;
  274.  
  275.   /* Now OK to enable signal catcher. */
  276. #ifdef NEED_SIGNAL_CATCHER
  277.   emethods = cinfo->emethods;
  278. #endif
  279.  
  280.   /* Set up default JPEG parameters. */
  281.   j_d_defaults(cinfo, TRUE);
  282.   requested_fmt = DEFAULT_FMT;    /* set default output file format */
  283.  
  284.   /* Scan command line options, adjust parameters */
  285.  
  286.   for (argn = 1; argn < argc; argn++) {
  287.     arg = argv[argn];
  288.     if (*arg != '-') {
  289.       /* Not a switch, must be a file name argument */
  290.       if (argn <= last_file_arg_seen)
  291.     continue;        /* ignore it if previously processed */
  292.       break;            /* else done parsing switches */
  293.     }
  294.     arg++;            /* advance past switch marker character */
  295.  
  296.     if (keymatch(arg, "blocksmooth", 1)) {
  297.       /* Enable cross-block smoothing. */
  298.       cinfo->do_block_smoothing = TRUE;
  299.  
  300.     } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
  301.            keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
  302.       /* Do color quantization. */
  303.       int val;
  304.  
  305.       if (++argn >= argc)    /* advance to next argument */
  306.     usage();
  307.       if (sscanf(argv[argn], "%d", &val) != 1)
  308.     usage();
  309.       cinfo->desired_number_of_colors = val;
  310.       cinfo->quantize_colors = TRUE;
  311.  
  312.     } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  313.       /* Enable debug printouts. */
  314.       /* On first -d, print version identification */
  315.       if (last_file_arg_seen == 0 && cinfo->emethods->trace_level == 0)
  316.     fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
  317.         JVERSION, JCOPYRIGHT);
  318.       cinfo->emethods->trace_level++;
  319.  
  320.     } else if (keymatch(arg, "gif", 1)) {
  321.       /* GIF output format. */
  322.       requested_fmt = FMT_GIF;
  323.  
  324.     } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
  325.       /* Force monochrome output. */
  326.       cinfo->out_color_space = CS_GRAYSCALE;
  327.  
  328.     } else if (keymatch(arg, "maxmemory", 1)) {
  329.       /* Maximum memory in Kb (or Mb with 'm'). */
  330.       long lval;
  331.       char ch = 'x';
  332.  
  333.       if (++argn >= argc)    /* advance to next argument */
  334.     usage();
  335.       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  336.     usage();
  337.       if (ch == 'm' || ch == 'M')
  338.     lval *= 1000L;
  339.       cinfo->emethods->max_memory_to_use = lval * 1000L;
  340.  
  341.     } else if (keymatch(arg, "nodither", 3)) {
  342.       /* Suppress dithering in color quantization. */
  343.       cinfo->use_dithering = FALSE;
  344.  
  345.     } else if (keymatch(arg, "onepass", 1)) {
  346.       /* Use fast one-pass quantization. */
  347.       cinfo->two_pass_quantize = FALSE;
  348.  
  349.     } else if (keymatch(arg, "pnm", 1)) {
  350.       /* PPM/PGM output format. */
  351.       requested_fmt = FMT_PPM;
  352.  
  353.     } else if (keymatch(arg, "rle", 1)) {
  354.       /* RLE output format. */
  355.       requested_fmt = FMT_RLE;
  356.  
  357.     } else if (keymatch(arg, "targa", 1)) {
  358.       /* Targa output format. */
  359.       requested_fmt = FMT_TARGA;
  360.  
  361.     } else {
  362.       usage();            /* bogus switch */
  363.     }
  364.   }
  365.  
  366.   return argn;            /* return index of next arg (file name) */
  367. }
  368.  
  369.  
  370. /*
  371.  * The main program.
  372.  */
  373.  
  374. GLOBAL int
  375. main (int argc, char **argv)
  376. {
  377.   struct Decompress_info_struct cinfo;
  378.   struct Decompress_methods_struct dc_methods;
  379.   struct External_methods_struct e_methods;
  380.   int file_index;
  381.  
  382.   /* On Mac, fetch a command line. */
  383. #ifdef THINK_C
  384.   argc = ccommand(&argv);
  385. #endif
  386.  
  387.   progname = argv[0];
  388.  
  389.   /* Set up links to method structures. */
  390.   cinfo.methods = &dc_methods;
  391.   cinfo.emethods = &e_methods;
  392.  
  393.   /* Install, but don't yet enable signal catcher. */
  394. #ifdef NEED_SIGNAL_CATCHER
  395.   emethods = NULL;
  396.   signal(SIGINT, signal_catcher);
  397. #ifdef SIGTERM            /* not all systems have SIGTERM */
  398.   signal(SIGTERM, signal_catcher);
  399. #endif
  400. #endif
  401.  
  402.   /* Scan command line: set up compression parameters, input & output files. */
  403.  
  404.   file_index = parse_switches(&cinfo, 0, argc, argv);
  405.  
  406. #ifdef TWO_FILE_COMMANDLINE
  407.  
  408.   if (file_index != argc-2) {
  409.     fprintf(stderr, "%s: must name one input and one output file\n", progname);
  410.     usage();
  411.   }
  412.   if ((cinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  413.     fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  414.     exit(EXIT_FAILURE);
  415.   }
  416.   if ((cinfo.output_file = fopen(argv[file_index+1], WRITE_BINARY)) == NULL) {
  417.     fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index+1]);
  418.     exit(EXIT_FAILURE);
  419.   }
  420.  
  421. #else /* not TWO_FILE_COMMANDLINE -- use Unix style */
  422.  
  423.   cinfo.input_file = stdin;    /* default input file */
  424.   cinfo.output_file = stdout;    /* always the output file */
  425.  
  426. #ifdef USE_SETMODE        /* need to hack file mode? */
  427.   setmode(fileno(stdin), O_BINARY);
  428.   setmode(fileno(stdout), O_BINARY);
  429. #endif
  430.  
  431.   if (file_index < argc-1) {
  432.     fprintf(stderr, "%s: only one input file\n", progname);
  433.     usage();
  434.   }
  435.   if (file_index < argc) {
  436.     if ((cinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  437.       fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  438.       exit(EXIT_FAILURE);
  439.     }
  440.   }
  441.  
  442. #endif /* TWO_FILE_COMMANDLINE */
  443.   
  444.   /* Set up to read a JFIF or baseline-JPEG file. */
  445.   /* A smarter UI would inspect the first few bytes of the input file */
  446.   /* to determine its type. */
  447. #ifdef JFIF_SUPPORTED
  448.   jselrjfif(&cinfo);
  449. #else
  450.   You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  451. #endif
  452.  
  453. #ifdef PROGRESS_REPORT
  454.   /* Start up progress display, unless trace output is on */
  455.   if (e_methods.trace_level == 0)
  456.     dc_methods.progress_monitor = progress_monitor;
  457. #endif
  458.  
  459.   /* Do it to it! */
  460.   jpeg_decompress(&cinfo);
  461.  
  462. #ifdef PROGRESS_REPORT
  463.   /* Clear away progress display */
  464.   if (e_methods.trace_level == 0) {
  465.     fprintf(stderr, "\r                \r");
  466.     fflush(stderr);
  467.   }
  468. #endif
  469.  
  470.   /* All done. */
  471.   exit(EXIT_SUCCESS);
  472.   return 0;            /* suppress no-return-value warnings */
  473. }
  474.